﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using LanDBom.Models;

namespace LanDBom.Controllers
{
    public class schedulesController : Controller
    {
        private LDbaseEntities db = new LDbaseEntities();

        // GET: schedules
        public ActionResult Index(string ming="")
        {
            var schedule = db.schedule.Include(s => s.libray).Where(s=>s.libray.LB_fne.Contains(ming)).ToList();
            return View( schedule);
        }

        // GET: schedules/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            schedule schedule = await db.schedule.FindAsync(id);
            if (schedule == null)
            {
                return HttpNotFound();
            }
            return View(schedule);
        }

        // GET: schedules/Create
        public ActionResult Create()
        {
            ViewBag.MA_ID = new SelectList(db.libray, "LB_ID", "LB_bai");
            return View();
        }

        // POST: schedules/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性；有关
        // 更多详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "SC_ID,MA_ID,SC_time,SC_Price,SC_Sun")] schedule schedule)
        {
            if (ModelState.IsValid)
            {
                db.schedule.Add(schedule);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.MA_ID = new SelectList(db.libray, "LB_ID", "LB_bai", schedule.MA_ID);
            return View(schedule);
        }

        // GET: schedules/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            schedule schedule = await db.schedule.FindAsync(id);
            if (schedule == null)
            {
                return HttpNotFound();
            }
            ViewBag.MA_ID = new SelectList(db.libray, "LB_ID", "LB_bai", schedule.MA_ID);
            return View(schedule);
        }

        // POST: schedules/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性；有关
        // 更多详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "SC_ID,MA_ID,SC_time,SC_Price,SC_Sun")] schedule schedule)
        {
            if (ModelState.IsValid)
            {
                db.Entry(schedule).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.MA_ID = new SelectList(db.libray, "LB_ID", "LB_bai", schedule.MA_ID);
            return View(schedule);
        }

        // GET: schedules/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            schedule schedule = await db.schedule.FindAsync(id);
            if (schedule == null)
            {
                return HttpNotFound();
            }
            return View(schedule);
        }

        // POST: schedules/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            schedule schedule = await db.schedule.FindAsync(id);
            db.schedule.Remove(schedule);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
